<!DOCTYPE html>


<html lang="zh-Hans">


<head>
  <meta charset="utf-8" />
   
  <meta name="keywords" content="代码,生活,杂记,思考" />
   
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    JQ-CSS实现老虎机抽奖效果 |  AURORA
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

<link rel="alternate" href="/atom.xml" title="AURORA" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-JQ-CSS实现老虎机抽奖效果"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  JQ-CSS实现老虎机抽奖效果
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/08/07/JQ-CSS%E5%AE%9E%E7%8E%B0%E8%80%81%E8%99%8E%E6%9C%BA%E6%8A%BD%E5%A5%96%E6%95%88%E6%9E%9C/" class="article-date">
  <time datetime="2020-08-06T16:00:00.000Z" itemprop="datePublished">2020-08-07 00:08:00</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">1.5k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">7 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h1><p>现在不少app都会有一些抽奖活动，热门的抽奖当属九宫格循环抽奖，而还有一种抽奖形式也是比较常见，比如老虎机的形式</p>
<h2 id="预期效果"><a href="#预期效果" class="headerlink" title="预期效果"></a>预期效果</h2><p><img src="https://img-blog.csdnimg.cn/20200804161721953.gif" alt=""><br>那么我们就用JQ和CSS实现一下这个效果。</p>
<a id="more"></a>
<h1 id="简单的实现思路"><a href="#简单的实现思路" class="headerlink" title="简单的实现思路"></a>简单的实现思路</h1><p>其实这个效果和实现一个轮播图是有异曲同工之妙的</p>
<p>假设现在有1，2，3三个奖品，我们可以在一个ul内使用多个序列拼接成一个长列表:1–2–3–1–2–3–1–2–3–1–2–3…..以此类推</p>
<p>每一组1–2–3视为一个集合，抽奖开始从第一个集合的某个奖品开始，到最后一个集合的某个奖品结束，中间使用transition加transform实现动效。</p>
<p>当每一次抽奖动效完成后ul会挺在最后一个集合的元素上，这个时候就需要让ul‘瞬移’一下，移动到当前这个奖项对应的第一个集合中的奖项，以便下一次抽奖</p>
<p>有了这个大概的思路，就可以着手于实现了</p>
<h2 id="先定义一下DOM结构"><a href="#先定义一下DOM结构" class="headerlink" title="先定义一下DOM结构"></a>先定义一下DOM结构</h2><p>我们实现的是上文提到的那个例子，但是为了简洁，我们的dom结构就先定义简单一点</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"game-content"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"game-goods-wrap"</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"game-goods-list"</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"game-goods"</span> <span class="attr">style</span>=<span class="string">"background: #ffffff;"</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"game-goods-box"</span> <span class="attr">id</span>=<span class="string">"game1"</span>&gt;</span></span><br><span class="line">					<span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">"game-goods-ul"</span>&gt;</span><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"game-goods"</span> <span class="attr">style</span>=<span class="string">"background: #ffffff;"</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"game-goods-box"</span> <span class="attr">id</span>=<span class="string">"game2"</span>&gt;</span></span><br><span class="line">					<span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">"game-goods-ul"</span>&gt;</span><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"game-goods"</span> <span class="attr">style</span>=<span class="string">"background: #ffffff;"</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"game-goods-box"</span> <span class="attr">id</span>=<span class="string">"game3"</span>&gt;</span></span><br><span class="line">					<span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">"game-goods-ul"</span>&gt;</span><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">"game-btn"</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="核心动效的实现"><a href="#核心动效的实现" class="headerlink" title="核心动效的实现"></a>核心动效的实现</h2><p>然后我们开始书写核心动效实现的封装函数，对外提供可以配置的接口option：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br></pre></td><td class="code"><pre><span class="line">luckGame: <span class="function"><span class="keyword">function</span>(<span class="params">options</span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> defaults = &#123;</span><br><span class="line">		<span class="string">'gameLen'</span>: <span class="string">'10'</span>, <span class="comment">// 奖品的数量</span></span><br><span class="line">		<span class="string">'game_pagesize'</span>: <span class="number">6</span>,<span class="comment">//生成多少圈同样的东西，调节速度用</span></span><br><span class="line">		<span class="string">'zj_arr'</span>: &#123; <span class="comment">//中奖数组，第一个表示是否中奖，第二个中奖号码</span></span><br><span class="line">			<span class="string">'is_win'</span>:<span class="number">1</span>,</span><br><span class="line">			<span class="string">'number'</span>:<span class="number">8</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;;</span><br><span class="line">	<span class="keyword">var</span> settings = <span class="built_in">Object</span>.assgin(defaults, options);</span><br><span class="line">	w_config=&#123;</span><br><span class="line">		<span class="string">'w'</span>:$(<span class="built_in">window</span>).width(),</span><br><span class="line">		<span class="string">'h'</span>:$(<span class="built_in">window</span>).height()</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">var</span> gameArr=[]; <span class="comment">// 奖品滚动的列表</span></span><br><span class="line">	<span class="keyword">var</span> gameLen=settings.gameLen; <span class="comment">// 奖品的种类数量</span></span><br><span class="line">	<span class="keyword">var</span> game_list_h=<span class="string">''</span>; <span class="comment">// 奖品滚动列表的高度</span></span><br><span class="line">	<span class="keyword">var</span> game_init=[]; <span class="comment">// 页面载入时随机的选择状态</span></span><br><span class="line">	<span class="keyword">var</span> game_list_item_h=<span class="number">0</span>; <span class="comment">// 单个奖品元素的高度</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//每次进来随机3个数字，来启动当前的选择的状态</span></span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</span><br><span class="line">		game_init.push(<span class="built_in">Math</span>.floor(<span class="built_in">Math</span>.random() * gameLen));</span><br><span class="line">	&#125;</span><br><span class="line">	createGame();</span><br><span class="line">	$(<span class="built_in">window</span>).resize(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;	</span><br><span class="line">		createGame();</span><br><span class="line">	&#125;)</span><br><span class="line">	<span class="comment">// 初始化游戏</span></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">createGame</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		getHeight();</span><br><span class="line">		setLi();</span><br><span class="line">		pushLi(gameArr);</span><br><span class="line">		start();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">getHeight</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		w_config=&#123;</span><br><span class="line">			<span class="string">'w'</span>:$(<span class="built_in">window</span>).width(),</span><br><span class="line">			<span class="string">'h'</span>:$(<span class="built_in">window</span>).height()</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">// 由于抽奖机本身的大小根据屏幕大小自适应，所以奖品高度需要计算</span></span><br><span class="line">		<span class="comment">// 实际场景下一般都是定高的</span></span><br><span class="line">		game_list_item_h=(w_config.w*<span class="number">320</span>/<span class="number">750</span>*<span class="number">0.5</span>*<span class="number">0.7</span>).toFixed(<span class="number">2</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">//设置奖品</span></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">setLi</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="comment">// 拼接实际的滚动数组</span></span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">1</span>; j &lt;= settings.game_pagesize; j++) &#123;</span><br><span class="line">			<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">1</span>; i &lt;= gameLen; i++) &#123;</span><br><span class="line">				gameArr.push(&#123;<span class="string">'type'</span>:j,<span class="string">'index'</span>:i,<span class="string">'src'</span>:<span class="string">'images/'</span>+i+<span class="string">'.png'</span>&#125;);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//写入，初始化奖品的滚动</span></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">pushLi</span>(<span class="params">arr</span>)</span>&#123;</span><br><span class="line">		<span class="keyword">var</span> html_str=<span class="string">''</span>;</span><br><span class="line">		<span class="comment">// 根据滚动数组生成li</span></span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">			html_str+=<span class="string">'&lt;li style="height:'</span>+game_list_item_h+<span class="string">'px" data-index="'</span> + arr[i][<span class="string">'index'</span>]+<span class="string">'" data-type="'</span>+arr[i][<span class="string">'type'</span>]+<span class="string">'"&gt;&lt;img src="'</span> + arr[i][<span class="string">'src'</span>]+<span class="string">'"&gt;&lt;/li&gt;'</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		$(<span class="string">".game-goods-ul"</span>).each(<span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>&#123;</span><br><span class="line">			$(<span class="keyword">this</span>).empty().append(html_str);</span><br><span class="line">			game_list_h=$(<span class="keyword">this</span>).height();</span><br><span class="line">			<span class="built_in">console</span>.log(<span class="string">'game_list_item_h'</span>,game_init);</span><br><span class="line">			y=game_list_item_h*game_init[e];</span><br><span class="line">			</span><br><span class="line">			$(<span class="keyword">this</span>).css(&#123;</span><br><span class="line">				<span class="string">'transition-duration'</span>: <span class="string">'0ms'</span>,</span><br><span class="line">				<span class="string">'transform'</span>:<span class="string">'translate(0px, -'</span>+y+<span class="string">'px) translateZ(0px)'</span></span><br><span class="line">			&#125;)</span><br><span class="line">		&#125;);</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 开始抽奖</span></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">start</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="comment">// 这一块是点击开始动效</span></span><br><span class="line">		<span class="comment">// 但实际场景中一般是点击之后和后台进行交互</span></span><br><span class="line">		<span class="comment">// 获取到中奖信息之后直接开始动效</span></span><br><span class="line">        <span class="comment">// 组件本身不负责点击事件</span></span><br><span class="line">		$(<span class="string">".game-btn"</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">			<span class="comment">//如果中奖</span></span><br><span class="line">			<span class="keyword">if</span>(settings.zj_arr.is_win==<span class="number">1</span>)</span><br><span class="line">			&#123;</span><br><span class="line">				</span><br><span class="line">				$(<span class="string">".game-goods-ul"</span>).each(<span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">					setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">						y=(settings.zj_arr.number+settings.gameLen*(settings.game_pagesize<span class="number">-1</span>))*game_list_item_h;</span><br><span class="line">						$(<span class="string">".game-goods-ul"</span>).eq(e).css(&#123;</span><br><span class="line">							<span class="string">'transition-duration'</span>: <span class="string">'5000ms'</span>,</span><br><span class="line">							<span class="string">'transform'</span>:<span class="string">'translate(0px, -'</span>+y+<span class="string">'px) translateZ(0px)'</span></span><br><span class="line">						&#125;)</span><br><span class="line">					&#125;, e*<span class="number">300</span>);</span><br><span class="line">					<span class="comment">// 判断最后面是否完毕</span></span><br><span class="line">					<span class="comment">// 抽奖动画完成后回到type=1的地方，等待下一次抽奖</span></span><br><span class="line">					$(<span class="string">"#game3"</span>).find(<span class="string">".game-goods-ul"</span>).on(<span class="string">"webkitTransitionEnd"</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">						y=settings.zj_arr.number*game_list_item_h;</span><br><span class="line">						$(<span class="string">".game-goods-ul"</span>).css(&#123;</span><br><span class="line">							<span class="string">'transition-duration'</span>: <span class="string">'0ms'</span>,</span><br><span class="line">							<span class="string">'transform'</span>:<span class="string">'translate(0px, -'</span>+y+<span class="string">'px) translateZ(0px)'</span></span><br><span class="line">						&#125;)</span><br><span class="line">						$(<span class="string">"#game3"</span>).find(<span class="string">".game-goods-ul"</span>).unbind(<span class="string">"webkitTransitionEnd"</span>);</span><br><span class="line">					&#125;)</span><br><span class="line">				&#125;)</span><br><span class="line">				</span><br><span class="line">			&#125;<span class="keyword">else</span></span><br><span class="line">			&#123;</span><br><span class="line">				<span class="comment">// 不中奖的时候哦生成随记停留的奖项，但是确保不能三个奖项一样</span></span><br><span class="line">				numrand=randNum2();</span><br><span class="line">				<span class="built_in">console</span>.log(numrand);</span><br><span class="line">				<span class="comment">// 不中奖的时候</span></span><br><span class="line">				$(<span class="string">".game-goods-ul"</span>).each(<span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>&#123;</span><br><span class="line">					y2=(numrand[<span class="number">0</span>])*game_list_item_h;</span><br><span class="line">					y3=(numrand[<span class="number">1</span>])*game_list_item_h;</span><br><span class="line">					y4=(numrand[<span class="number">2</span>])*game_list_item_h;</span><br><span class="line">					setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">						y=(numrand[e]+settings.gameLen*(settings.game_pagesize<span class="number">-1</span>))*game_list_item_h;</span><br><span class="line">						$(<span class="string">".game-goods-ul"</span>).eq(e).css(&#123;</span><br><span class="line">							<span class="string">'transition-duration'</span>: <span class="string">'5000ms'</span>,</span><br><span class="line">							<span class="string">'transform'</span>:<span class="string">'translate(0px, -'</span>+y+<span class="string">'px) translateZ(0px)'</span></span><br><span class="line">						&#125;)</span><br><span class="line">					&#125;, e*<span class="number">300</span>);</span><br><span class="line">					<span class="comment">//判断最后面是否完毕</span></span><br><span class="line">					<span class="comment">// 抽奖动画完成后回到type=1的地方，等待下一次抽奖</span></span><br><span class="line">					$(<span class="string">"#game3"</span>).find(<span class="string">".game-goods-ul"</span>).on(<span class="string">"webkitTransitionEnd"</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">						</span><br><span class="line">						$(<span class="string">".game-goods-ul"</span>).eq(<span class="number">2</span>).css(&#123;</span><br><span class="line">							<span class="string">'transition-duration'</span>: <span class="string">'00ms'</span>,</span><br><span class="line">							<span class="string">'transform'</span>:<span class="string">'translate(0px, -'</span>+y4+<span class="string">'px) translateZ(0px)'</span></span><br><span class="line">						&#125;)</span><br><span class="line">						$(<span class="string">"#game3"</span>).find(<span class="string">".game-goods-ul"</span>).unbind(<span class="string">"webkitTransitionEnd"</span>);</span><br><span class="line">					&#125;)</span><br><span class="line">					$(<span class="string">"#game2"</span>).find(<span class="string">".game-goods-ul"</span>).on(<span class="string">"webkitTransitionEnd"</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">						</span><br><span class="line">						$(<span class="string">".game-goods-ul"</span>).eq(<span class="number">1</span>).css(&#123;</span><br><span class="line">							<span class="string">'transition-duration'</span>: <span class="string">'00ms'</span>,</span><br><span class="line">							<span class="string">'transform'</span>:<span class="string">'translate(0px, -'</span>+y3+<span class="string">'px) translateZ(0px)'</span></span><br><span class="line">						&#125;)</span><br><span class="line">						$(<span class="string">"#game2"</span>).find(<span class="string">".game-goods-ul"</span>).unbind(<span class="string">"webkitTransitionEnd"</span>);</span><br><span class="line">					&#125;)</span><br><span class="line">					$(<span class="string">"#game1"</span>).find(<span class="string">".game-goods-ul"</span>).on(<span class="string">"webkitTransitionEnd"</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">						</span><br><span class="line">						$(<span class="string">".game-goods-ul"</span>).eq(<span class="number">0</span>).css(&#123;</span><br><span class="line">							<span class="string">'transition-duration'</span>: <span class="string">'00ms'</span>,</span><br><span class="line">							<span class="string">'transform'</span>:<span class="string">'translate(0px, -'</span>+y2+<span class="string">'px) translateZ(0px)'</span></span><br><span class="line">						&#125;)</span><br><span class="line">						$(<span class="string">"#game1"</span>).find(<span class="string">".game-goods-ul"</span>).unbind(<span class="string">"webkitTransitionEnd"</span>);</span><br><span class="line">					&#125;)</span><br><span class="line">				&#125;)</span><br><span class="line"></span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">			</span><br><span class="line">		&#125;)</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 生成不中奖时候的随机显示序列</span></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">randNum2</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		a=<span class="built_in">Math</span>.floor(<span class="built_in">Math</span>.random() * gameLen);</span><br><span class="line">		b=<span class="built_in">Math</span>.floor(<span class="built_in">Math</span>.random() * gameLen);</span><br><span class="line">		c=<span class="built_in">Math</span>.floor(<span class="built_in">Math</span>.random() * gameLen);</span><br><span class="line">		arr=[];</span><br><span class="line">		<span class="keyword">if</span>(a==b)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">return</span> randNum2();</span><br><span class="line">		&#125;<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">return</span> arr=[a,b,c];</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="调用"><a href="#调用" class="headerlink" title="调用"></a>调用</h2><p>然后我们就可以像这样传入配置，这一块实际场景中是点击按钮过后再传入配置，当配置传入之后调用函数，直接开始动画：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">luckGame(&#123;</span><br><span class="line">	<span class="string">'zj_arr'</span>: &#123;</span><br><span class="line">		<span class="string">'is_win'</span>:<span class="number">1</span>,</span><br><span class="line">		<span class="string">'number'</span>:<span class="number">9</span><span class="comment">//从0算起，就是10了</span></span><br><span class="line">	&#125;,</span><br><span class="line">	gameLen:<span class="number">10</span>,<span class="comment">//产品抽奖数量，</span></span><br><span class="line">	game_pagesize:<span class="number">6</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>以上就是核心的动效实现，如果是使用vue或者react之类的框架，那么我们则只需要关注数据本身就行，由数据去驱动视图，省去了繁杂的DOM操作，整个过程会更加简洁。</p>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>Copyright： </strong>
          Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/08/13/typescript%E7%B1%BB%E5%9E%8B%E6%96%AD%E8%A8%80%E8%A7%A3%E5%86%B3%E4%B8%80%E4%BA%9B%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            typescript类型断言解决一些常见问题
          
        </div>
      </a>
    
    
      <a href="/2020/08/03/%E6%B7%B1%E5%85%A5%E6%B5%85%E5%87%BA-Webpack%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%89%EF%BC%89/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">深入浅出-Webpack学习笔记（三）</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "2eBxdPB3eEwraN7IpwFckzeL-gzGzoHsz",
    app_key: "VGgCefQEluVxHdyMsm1Dpeih",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "评论一下我的文章吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2020
        <i class="ri-heart-fill heart_icon"></i> AURORA_ZXH
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        Powered by <a href="https://hexo.io" target="_blank">Hexo</a>
        <span class="division">|</span>
        Theme - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/avatar.jpg" alt="AURORA"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="http://aurora-zxh.lofter.com/" target="_blank" rel="noopener">随记</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/link">友链</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.png">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wxpay.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>